Title Banner

Previous Book Contents Book Index Next

Inside Macintosh: OpenDoc Cookbook / Part - Appendixes
Appendix A - OpenDoc Utilities


Memory Management (ODMemory)

This section describes the OpenDoc memory manager utility, the Mac OS implementation of which resides in the files ODMemory. h and ODMemory.cpp.

OpenDoc includes a memory management utility that you can use for allocating and manipulating memory as needed by your parts. On each platform, the OpenDoc memory manager supplements the capabilities of the platform's own memory manager; you can use platform memory manager calls alone, you can use the OpenDoc memory manager alone, or you can use both as needed.

The OpenDoc memory manager is a fast and very space-efficient memory allocator. It is a utility library used by OpenDoc but independent of it. The OpenDoc memory manager's only requirement is that its clients use a procedural shared library mechanism, such as CFM on the Mac OS or DLL on Windows.

The OpenDoc memory manager works with but is not dependent upon SOM. When both are installed, the OpenDoc memory manager takes over the SOM memory management routines; in that case, calls to functions such as SOMMalloc and SOMFree use the OpenDoc memory manager.

For a brief introduction to SOM, refer to Appendix B, "System Object Model." For information about the SOM memory manager, see SOMobjects Developer Toolkit Users Guide and SOMobjects Developer Toolkit Programmers Reference Manual from IBM.

Allocating Heaps

A heap is a space in which blocks of memory of arbitrary size can be allocated. All blocks allocated by the OpenDoc memory manager (other than handles) are in one of its heaps. When the OpenDoc memory manager initializes itself, it creates a heap for you; you can create additional heaps if you want to. You can also delete heaps, and deleting a heap with blocks still in it is both legal and faster than deleting all the blocks individually.

All storage used by the OpenDoc memory manager originally comes from the operating system's platform-specific memory manager. The OpenDoc memory manager gets memory for its heaps from the platform memory manager in large chunks (typically 32 KB or greater), and then subdivides these chunks as needed to allocate blocks. When a heap runs out of room, the OpenDoc memory manager asks the platform memory manager for another chunk, and when the OpenDoc memory manager frees all blocks in a chunk, it returns the entire chunk to the platform memory manager.

The data type that represents a heap (MemHeap) is opaque; no internal structure is visible to you. You refer to heaps using pointers, and you can operate on them only with the OpenDoc memory manager functions.

Memory for a heap can come from one of three places:

On non-Mac OS platforms, application and temporary memory may be identical; on the Mac OS, however, temporary memory is important because very little application memory may be available in the fixed-size partition available to an OpenDoc process. For cross-platform code, therefore, it is better to specify temporary memory.

These are the principal heap-manipulation functions provided by the OpenDoc memory manager:

Using multiple heaps can be convenient for your part editor, although it is not quite as efficient as storing everything in one heap (because free memory in one heap is not available to another). However, allocating a heap for temporary use and then deleting it when done can help reduce memory fragmentation, since deleting the heap leaves a small number of large free blocks, rather than a large number of small ones.

Allocating Nonrelocatable Blocks

Memory within a heap is allocated nonrelocatable blocks. The interface for creating and operating on these blocks is similar to the ANSI C memory API, which is similar to that used by SOM. In fact, the SOM memory calls are rerouted to these routines, so that calling SOMMalloc, for example, is identical to calling MMAllocate.

These are the principal block-allocation functions provided by the OpenDoc memory manager:

You should clear the is-object flag before freeing any block, because the debugging configuration of the OpenDoc memory manager warns you if you free a block containing an object. SOM objects that inherit from ODObject (the OpenDoc root object class) automatically set the is-object flag when created and clear it when deleted.

Allocating Relocatable Blocks (Handles)

For convenience, the OpenDoc memory manager also provides operations for allocating relocatable blocks, referenced via handles. These blocks are allocated directly by the platform's memory manager, not by the OpenDoc memory manager, and they don't reside in heaps managed by the OpenDoc memory manager. However, you can still specify the same types of locations.

Because relocatable blocks are allocated by the platform's memory manager, an OpenDoc handle (type MMHandle) is the same as a platform-specific handle and can be passed to operating system routines that take handles; likewise, a handle allocated by an operating system routine can be passed to any of the OpenDoc memory manager routines that take a parameter of type MMHandle.

These are the principal handle-allocation functions provided by the OpenDoc memory manager:

IMPORTANT
Calls to MMLockHandle and MMUnlockHandle do not nest. The first call to MMUnlockHandle unlocks the block (and invalidates any pointers to its contents) no matter how many times MMLockHandle has been called.

Memory Debugging

There are two configurations of the OpenDoc memory manager utility: regular and debugging. During development, if you link with the debugging configuration you can use its extra functions to help debug your code's memory management. These functions allow you to detect whether you are passing illegal values to the OpenDoc memory manager or overwriting heap data outside of blocks. You can also determine whether a given block is valid, and you can collect statistics on a heap as a whole or on all blocks in a heap.

Besides providing these extra routines, the debugging configuration also performs more internal checking of function parameters and data structures; this makes it slower but better able to detect problems.

These are the principal debugging functions provided by the debugging configuration of the OpenDoc memory manager:


Previous Book Contents Book Index Next

© Apple Computer, Inc.
16 JUL 1996




Navigation graphic, see text links

Main | Page One | What's New | Apple Computer, Inc. | Find It | Contact Us | Help